﻿using HalconDotNet;
using IniReadWrite;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VisionRoc.Tool;

namespace VisionRoc.Core
{
    public class SetEllipse
    {
        public string EllipsePath;
        public SetEllipse(string _Path)
        {
            HOperatorSet.GenEmptyObj(out Contour);
            HOperatorSet.GenEmptyObj(out Callipers);
            HOperatorSet.GenEmptyObj(out Coordinate);
            //判断文件路径是否存在
            if (!Directory.Exists(_Path))
            {
                Directory.CreateDirectory(_Path);
            }
            EllipsePath = _Path + "\\Ellipse.ini";
            if (File.Exists(EllipsePath))
            {
                ReadParameter();
            }

        }

        /// <summary>
        /// 绘制椭圆时Row坐标
        /// </summary>
        public double Row;

        /// <summary>
        /// 绘制椭圆是Col坐标
        /// </summary>
        public double Col;

        /// <summary>
        /// 绘制椭圆弧度
        /// </summary>
        public double Phi;

        /// <summary>
        /// 绘制椭圆半径1
        /// </summary>
        public double Raduise1;

        /// <summary>
        /// 绘制椭圆半径2 
        /// </summary>
        public double Raduise2;


        [Browsable(true), Description("卡尺数量：该值的一般区间: 1 ≤ Elements ≤ 100000"), Category("工具参数"), ReadOnly(false)]
        public int Elements { get; set; }

        [Browsable(true), Description("卡尺高度：\r\n建议值: 30, 60, 90, 120"), Category("工具参数"), ReadOnly(false)]
        public int DetectHeight { get; set; }

        [Browsable(true), Description("卡尺宽度：\r\n建议值: 5, 10, 15, 20, 25, 30"), Category("工具参数"), ReadOnly(false)]
        public int DetectWidth { get; set; }

        [Browsable(true), Description("滤波因子：高斯滤波因子\r\n建议值: 0.5, 1, 1.5, 2, 2.5, 3"), Category("工具参数"), ReadOnly(false)]
        public double Sigma { get; set; }

        [Browsable(true), Description("边缘幅度：边缘幅度阈值\r\n建议值: 5, 10, 15, 20, 25, 30, 40"), Category("工具参数"), ReadOnly(false)]
        public int Threshold { get; set; }

        [Browsable(true), Description("边缘极性：positive表示由黑到白,\r\nnegative表示由白到黑,\r\nall表示以上两种方向"), TypeConverter(typeof(GenderItem1)), Category("工具参数"), ReadOnly(false)]
        public string Transition { get; set; }

        [Browsable(true), Description("点选择：first表示选择第一点,\r\nlast表示选择最后一点,\r\nmax表示选择边缘幅度最强点"), TypeConverter(typeof(GenderItem_Two)), Category("工具参数"), ReadOnly(false)]
        public string Select { get; set; }



        /// <summary>
        /// 显示点位置
        /// </summary>
        public bool DisplayLoction;

        /// <summary>
        /// 显示线段
        /// </summary>
        public bool DisplayEllipse;


        /// <summary>
        /// 显示卡尺
        /// </summary>
        public bool DisplayCalib;

        /// <summary>
        /// 线宽
        /// </summary>
        public int LineWidth;

        /// <summary>
        /// 位置大小
        /// </summary>
        public int CrossSize;

        /// <summary>
        /// 位置颜色
        /// </summary>
        public string CrossColor;

        /// <summary>
        /// 卡尺颜色
        /// </summary>
        public string CalibColor;

        /// <summary>
        /// 椭圆轮廓颜色
        /// </summary>
        public string EllipseColor;





        /// <summary>
        /// 拟合输出图像区域
        /// </summary>
        HObject Contour;

        /// <summary>
        /// 输出卡尺图行
        /// </summary>
        HObject Callipers;

        /// <summary>
        /// 有效点位 Coordinate
        /// </summary>
        HObject Coordinate;

       public HTuple FitRow = new HTuple();

        public HTuple FitCol = new HTuple();

        public HTuple FitPhi = new HTuple();

        public HTuple FitRaduise1 = new HTuple();

        public HTuple FitRaduise2 = new HTuple();

        public HTuple EllipserOW = new HTuple();

        public HTuple EllipseColumn = new HTuple();

        public void Fit_Ellipse(HObject Image, HWindow hWindow, HTuple Row1, HTuple Col1, HTuple Phi1, HTuple Raduise_1, HTuple Raduise_2)
        {
            Contour.Dispose(); Callipers.Dispose(); Coordinate.Dispose(); FitRow.Dispose(); FitCol.Dispose();
            EllipserOW.Dispose(); EllipseColumn.Dispose(); FitPhi.Dispose(); FitRaduise1.Dispose(); FitRaduise2.Dispose();
            MyHOperatorSet.Spoke_Ellipse(Image, out Contour, out Callipers, out Coordinate, Row1, Col1, Phi1,
                Raduise_1, Raduise_2, DetectHeight, DetectWidth, Sigma, Threshold, Elements, Transition, Select,
               out FitRow, out FitCol, out EllipserOW, out EllipseColumn, out FitPhi, out FitRaduise1, out FitRaduise2);
            if (EllipserOW.D > 0)
            {
                if (DisplayLoction)
                {
                    HOperatorSet.SetColor(hWindow, CrossColor);
                    //Cross.Dispose();
                    //HOperatorSet.GenCrossContourXld(out Cross, ResultRow, ResultCol, CrossSize, 0);
                    HOperatorSet.DispObj(Coordinate, hWindow);
                }

                if (DisplayEllipse)
                {
                    HOperatorSet.SetLineWidth(hWindow, LineWidth);
                    HOperatorSet.SetColor(hWindow, EllipseColor);
                    HOperatorSet.DispObj(Contour, hWindow);
                }

                if (DisplayCalib)
                {
                    HOperatorSet.SetLineWidth(hWindow, 1);
                    HOperatorSet.SetColor(hWindow, CalibColor);
                    HOperatorSet.DispObj(Callipers, hWindow);
                }
                HOperatorSet.SetColored(hWindow, 12);
                HOperatorSet.SetLineWidth(hWindow, 2);
                HOperatorSet.SetColor(hWindow, "red");
            }
            else
            {
                MyHOperatorSet.DispMeassge(hWindow, "椭圆拟合失败", "window", "center", "center", 20, "red");
            }

        }


        public void ReadParameter()
        {
            try
            {
                Elements = Convert.ToInt32(IniAPI.INIGetStringValue(EllipsePath, "椭圆参数", "Elements", "0"));
                DetectHeight = Convert.ToInt32(IniAPI.INIGetStringValue(EllipsePath, "椭圆参数", "DetectHeight", "0"));
                DetectWidth = Convert.ToInt32(IniAPI.INIGetStringValue(EllipsePath, "椭圆参数", "DetectWidth", "0"));
                Sigma = Convert.ToDouble(IniAPI.INIGetStringValue(EllipsePath, "椭圆参数", "Sigma", "0"));
                Threshold = Convert.ToInt32(IniAPI.INIGetStringValue(EllipsePath, "椭圆参数", "Threshold", "0"));
                Transition = IniAPI.INIGetStringValue(EllipsePath, "椭圆参数", "Transition", "0");
                Select = IniAPI.INIGetStringValue(EllipsePath, "椭圆参数", "Select", "0");

                //FitRow = Convert.ToDouble(IniAPI.INIGetStringValue(EllipsePath, "拟合椭圆参数", "FitRow", "0"));
                //FitCol = Convert.ToDouble(IniAPI.INIGetStringValue(EllipsePath, "拟合椭圆参数", "FitCol", "0"));
                //FitPhi = Convert.ToDouble(IniAPI.INIGetStringValue(EllipsePath, "拟合椭圆参数", "FitPhi", "0"));
                //FitRaduise1 = Convert.ToDouble(IniAPI.INIGetStringValue(EllipsePath, "拟合椭圆参数", "FitRaduise1", "0"));
                //FitRaduise2 = Convert.ToDouble(IniAPI.INIGetStringValue(EllipsePath, "拟合椭圆参数", "FitRaduise2", "0"));
                //EllipserOW = Convert.ToDouble(IniAPI.INIGetStringValue(EllipsePath, "拟合椭圆参数", "EllipserOW", "0"));
                //EllipseColumn = Convert.ToDouble(IniAPI.INIGetStringValue(EllipsePath, "拟合椭圆参数", "EllipseColumn", "0"));

                if (Boolean.TryParse(IniAPI.INIGetStringValue(EllipsePath, "显示设置", "DisplayLoction", "0"), out bool a))
                {
                    DisplayLoction = Convert.ToBoolean(IniAPI.INIGetStringValue(EllipsePath, "显示设置", "DisplayLoction", "0"));
                }
                else
                {
                    DisplayLoction = a;
                }

                if (Boolean.TryParse(IniAPI.INIGetStringValue(EllipsePath, "显示设置", "DisplayCircle", "0"), out bool b))
                {
                    DisplayEllipse = Convert.ToBoolean(IniAPI.INIGetStringValue(EllipsePath, "显示设置", "DisplayCircle", "0"));
                }
                else
                {
                    DisplayEllipse = b;
                }

                if (Boolean.TryParse(IniAPI.INIGetStringValue(EllipsePath, "显示设置", "DisplayCalib", "0"), out bool c))
                {
                    DisplayCalib = Convert.ToBoolean(IniAPI.INIGetStringValue(EllipsePath, "显示设置", "DisplayCalib", "0"));
                }
                else
                {
                    DisplayCalib = c;
                }
                LineWidth = Convert.ToInt32(IniAPI.INIGetStringValue(EllipsePath, "显示设置", "LineWidth", "0"));
                EllipseColor = IniAPI.INIGetStringValue(EllipsePath, "显示设置", "CircleColor", "0");
                CrossSize = IniAPI.INIGetStringValue(EllipsePath, "显示设置", "CrossSize", "0").ToInt32();
                CrossColor = IniAPI.INIGetStringValue(EllipsePath, "显示设置", "CrossColor", "0");
                CalibColor = IniAPI.INIGetStringValue(EllipsePath, "显示设置", "CalibColor", "0");
            }
            catch
            {

            }
          
        }

        public void WriteParameter()
        {
            IniAPI.INIWriteValue(EllipsePath, "椭圆参数", "Elements", Elements.ToString());
            IniAPI.INIWriteValue(EllipsePath, "椭圆参数", "DetectHeight", DetectHeight.ToString());
            IniAPI.INIWriteValue(EllipsePath, "椭圆参数", "DetectWidth", DetectWidth.ToString());
            IniAPI.INIWriteValue(EllipsePath, "椭圆参数", "Sigma", Sigma.ToString());
            IniAPI.INIWriteValue(EllipsePath, "椭圆参数", "Threshold", Threshold.ToString());
            IniAPI.INIWriteValue(EllipsePath, "椭圆参数", "Transition", Transition.ToString());
            IniAPI.INIWriteValue(EllipsePath, "椭圆参数", "Select", Select.ToString());

            IniAPI.INIWriteValue(EllipsePath, "拟合椭圆参数", "FitRow", FitRow.ToString());
            IniAPI.INIWriteValue(EllipsePath, "拟合椭圆参数", "FitCol", FitCol.ToString());
            IniAPI.INIWriteValue(EllipsePath, "拟合椭圆参数", "FitPhi", FitPhi.ToString());
            IniAPI.INIWriteValue(EllipsePath, "拟合椭圆参数", "FitRaduise1", FitRaduise1.ToString());
            IniAPI.INIWriteValue(EllipsePath, "拟合椭圆参数", "FitRaduise2", FitRaduise2.ToString());
            IniAPI.INIWriteValue(EllipsePath, "拟合椭圆参数", "EllipserOW", EllipserOW.ToString());
            IniAPI.INIWriteValue(EllipsePath, "拟合椭圆参数", "EllipseColumn", EllipseColumn.ToString());

            IniAPI.INIWriteValue(EllipsePath, "显示设置", "DisplayLoction", DisplayLoction.ToString());
            IniAPI.INIWriteValue(EllipsePath, "显示设置", "DisplayCircle", DisplayEllipse.ToString());
            IniAPI.INIWriteValue(EllipsePath, "显示设置", "DisplayCalib", DisplayCalib.ToString());


            IniAPI.INIWriteValue(EllipsePath, "显示设置", "LineWidth", LineWidth.ToString());
            IniAPI.INIWriteValue(EllipsePath, "显示设置", "CircleColor", EllipseColor.ToString());
            IniAPI.INIWriteValue(EllipsePath, "显示设置", "CrossSize", CrossSize.ToString());
            IniAPI.INIWriteValue(EllipsePath, "显示设置", "CrossColor", CrossColor.ToString());
            IniAPI.INIWriteValue(EllipsePath, "显示设置", "CalibColor", CalibColor.ToString());
        }

    }
}
